package com.acwing.partition2;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/1/2 15:35
 */
public class AC167木棒 {

    private static final int N = 70;
    private static int[] weight = new int[N];
    private static int n = 0, sum = 0;
    private static boolean[] visited = new boolean[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        while (true) {
            n = Integer.parseInt(reader.readLine());
            if (n == 0) break;
            String[] s = reader.readLine().split("\\s+");
            for (int i = 0; i < n; i++) {
                weight[i] = Integer.parseInt(s[i]);
                visited[i] = false;
            }
            writer.write(resolve() + "\n");
        }
        writer.flush();
    }

    private static int resolve() {
        //剪枝1：优化搜索顺序
        Arrays.sort(weight, 0, n);
        for (int i = 0; i < n / 2; i++) {
            int temp = weight[i];
            weight[i] = weight[n - i - 1];
            weight[n - i - 1] = temp;
        }
        sum = 0;
        for (int i = 0; i < n; i++) sum += weight[i];
        for (int length = weight[0]; length <= sum; length++) {
            //尝试以当前枚举的长度进行搜索
            if (sum % length == 0 && dfs(0, 0, 0, length)) {
                return length;
            }
        }
        return -1;
    }

    private static boolean dfs(int stick, int curr, int start, int length) {
        if (stick * length == sum) return true;
        //当前木棍的长度达到了规定的长度，就递归拼接下一个木棍
        if (curr == length) return dfs(stick + 1, 0, 0, length);
        //从start的开始放
        for (int i = start; i < n; i++) {
            //可行性剪枝
            if (visited[i] || curr + weight[i] > length) continue;
            visited[i] = true;
            if (dfs(stick, curr + weight[i], i + 1, length)) return true;
            visited[i] = false;
            //剪枝1：如果当前木棒还没放，weight[i]都已经无法放下，那么剩下还没有放的所有木棒都不能放下它；
            //剪枝2：如果拼接当前stick刚好拼好，但是拼后面的木棒失败，说明该方案后面的所有木棒放入weight[i]都会失败
            if (curr == 0 || curr + weight[i] == length) return false;
            //当前长度weight[i]失败了，如果长度和它一样，也必定会失败
            int j = i;
            while (weight[j] == weight[i]) j++;
            i = j - 1;
        }
        return false;
    }
}
